<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<script src="//cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
<script src="//cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
SSO统一认证系统<br/>
用户名：<input id="name"/><br/>
<button id="login">登录</button>
</body>
<script>
    $(function(){
        getInfo();
        function getInfo(){
            var callbackUrl = window.location.search.substring(39);
            var tokenVal = getQueryVariable1(callbackUrl,"token");
            if (tokenVal && tokenVal != undefined && tokenVal != 'undefined') {
                $.removeCookie('token');
                $.removeCookie('token',{domain:'sso.com'});
            }
            var token = $.cookie('token');
            if (token) {
                var url = getQueryVariable("callback");
                url = urlDelP(url,"token");
                if (!url.endsWith("?")) {
                    url = url +"?";
                }
                window.location.href = url+"token="+token;
            }
        };

        $("button").click(function(){
            var name = $("#name").val();
            $.get("http://localhost:8080/login?name="+name,function(data,status){
                $.cookie('token',data, {domain:'sso.com'});
                var url = getQueryVariable("callback");
                url = urlDelP(url,"token");
                if (!url.endsWith("?")) {
                    url = url +"?";
                }
                window.location.href = url+"token="+data;
            });
        });

        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                if(pair[0] == variable){return pair[1];}
            }
            return(false);
        }
        function getQueryVariable1(url,variable) {
            var query = url;
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                if(pair[0] == variable){return pair[1];}
            }
            return(false);
        }

        function urlDelP(url,name){
            var urlArr = url.split('?');
            if(urlArr.length>1 && urlArr[1].indexOf(name)>-1){
                var query = urlArr[1];
                var obj = {}
                var arr = query.split("&");
                for (var i = 0; i < arr.length; i++) {
                    arr[i] = arr[i].split("=");
                    obj[arr[i][0]] = arr[i][1];
                };
                delete obj[name];
                var urlte = urlArr[0] +'?'+ JSON.stringify(obj).replace(/[\"\{\}]/g,"").replace(/\:/g,"=").replace(/\,/g,"&");
                return urlte;
            }else{
                return url;
            };
        }
    });
</script>
</html>